package com.hs.color.lamp;

import android.util.Log;

import com.chiemy.cardview.Utils;

public class SppCommandPacket {

	public static byte[] opcode = {0x60, (byte) 0xfd}; 
	
	public static byte[] COMM_DRIVE_PWM_INIT 			= {0x70, 0x00};
	public static byte[] COMM_DRIVE_SET_PWMPULSEWIDTH 	= {0x71, 0x00};
	public static byte[] COMM_DRIVE_SET_PWMPERIOD       = {0x72, 0x00};
	 
	public static byte[] COMM_DRIVE_SET_PWMTIME			= {0x73, 0x00};
	 
	public static byte[] COMM_DRIVE_PWM_CTRL			= {(byte) 0x80, 0x00};

	public static byte[] COMM_BRIDGE_TX 				= {0x50, 0x00};
	public static byte[] COMM_FM_FRQ_INI   				= {0x30, 0x00};
	public static byte[] COMM_FM_FRQ_SET   				= {0x31, 0x00};
	
	public static byte[] COMM_MEM_WRITE					= {0X41, 0X00};
	public static byte[] COMM_MEM_READ					= {0X40, 0X00};
	
	
	public static byte[] getReadMEMCtrlPacket(byte[] addr)
	{

		byte[] tmp = new byte[11];
		tmp[0] = 0x01;
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (0x07);
		System.arraycopy(COMM_MEM_READ, 0, tmp, 4, 2);
		tmp[6] = 0x04;
		System.arraycopy(addr, 0, tmp, 7, addr.length);
		
		return tmp;
	}
	
	
	
	public static byte[] getMcuCtrlPacket(byte[] addr, int tt)
	{
		byte[] data = new byte[4];
		Utils.int4_to_bytearray(tt, data);
		byte[] tmp = new byte[15];
		tmp[0] = 0x01;
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (0x0b);
		System.arraycopy(COMM_MEM_WRITE, 0, tmp, 4, 2);
		tmp[6] = 0x00;
		System.arraycopy(addr, 0, tmp, 7, addr.length);
		System.arraycopy(data, 0, tmp, 11, data.length);
		return tmp;
	}
	
	
	public static byte[] geAppClkCtrlPacket()
	{
		byte[] data = new byte[4];
		byte[] addr  = {0x0c, 0x10, 0x00, 0x40};
		//byte[] value = {(byte) 0xEF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
		int tt = 0x1018;//Utils.bytearray_to_int4(data);
		Utils.int4_to_bytearray(tt, data);
		byte[] tmp = new byte[15];
		tmp[0] = 0x01;
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (0x0b);
		System.arraycopy(COMM_MEM_WRITE, 0, tmp, 4, 2);
		tmp[6] = 0x00;
		System.arraycopy(addr, 0, tmp, 7, addr.length);
		System.arraycopy(data, 0, tmp, 11, data.length);
		return tmp;
	}
	
	public static byte[] getHardFaultCtrlPacket()
	{
		byte[] data = new byte[4];
		byte[] addr  = {0x0c, (byte) 0xed, 0x00, (byte) 0xe0};
		//byte[] value = {(byte) 0xEF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
		int tt = 0x05FA0004;//Utils.bytearray_to_int4(data);
		Utils.int4_to_bytearray(tt, data);
		byte[] tmp = new byte[15];
		tmp[0] = 0x01;
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (0x0b);
		System.arraycopy(COMM_MEM_WRITE, 0, tmp, 4, 2);
		tmp[6] = 0x00;
		System.arraycopy(addr, 0, tmp, 7, addr.length);
		System.arraycopy(data, 0, tmp, 11, data.length);
		return tmp;
	}
	
	public static byte[] getWatchdogCtrlPacket()
	{
		byte[] data = new byte[4];
		byte[] addr  = {0x1c, 0x00, 0x0d, 0x40};
		//byte[] value = {(byte) 0xEF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
		int tt = 3;//Utils.bytearray_to_int4(data);
		Utils.int4_to_bytearray(tt, data);
		byte[] tmp = new byte[15];
		tmp[0] = 0x01;
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (0x0b);
		System.arraycopy(COMM_MEM_WRITE, 0, tmp, 4, 2);
		tmp[6] = 0x00;
		System.arraycopy(addr, 0, tmp, 7, addr.length);
		System.arraycopy(data, 0, tmp, 11, data.length);
		return tmp;
	}
	
	
	public static byte[] getFlashEnablePacket(byte[] data)
	{
		byte[] addr  = {0x10, 0x10, 0x00, 0x40};
		//byte[] value = {(byte) 0xEF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
		int tt = Utils.bytearray_to_int4(data);
		tt = tt & (~((1 << 3) | (1 << 1))); 
		Utils.int4_to_bytearray(tt, data);
		Log.i("MCU", "fLASH: "+tt);
		byte[] tmp = new byte[15];
		tmp[0] = 0x01;
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (0x0b);
		System.arraycopy(COMM_MEM_WRITE, 0, tmp, 4, 2);
		tmp[6] = 0x00;
		System.arraycopy(addr, 0, tmp, 7, addr.length);
		System.arraycopy(data, 0, tmp, 11, data.length);
		return tmp;
	}
	
	public static byte[] getFlashDisablePacket(byte[] data)
	{
		byte[] addr  = {0x10, 0x10, 0x00, 0x40};
		//byte[] value = {(byte) 0xEF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
		int tt = Utils.bytearray_to_int4(data);
		tt = tt | (1 << 3) | (1 << 1); 
		Utils.int4_to_bytearray(tt, data);
		Log.i("MCU", "fLASH: "+tt);
		byte[] tmp = new byte[15];
		tmp[0] = 0x01;
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (0x0b);
		System.arraycopy(COMM_MEM_WRITE, 0, tmp, 4, 2);
		tmp[6] = 0x00;
		System.arraycopy(addr, 0, tmp, 7, addr.length);
		System.arraycopy(data, 0, tmp, 11, data.length);
		return tmp;
	}
	
	public static byte[] getFlashCtrlPacket_0(byte[] data)
	{
		byte[] addr  = {0x10, 0x10, 0x00, 0x40};
		//byte[] value = {(byte) 0xEF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
		int tt = Utils.bytearray_to_int4(data);
		Log.i("MCU", "fLASH: "+tt);
		tt = tt & (~(1 << 2)); 
		Utils.int4_to_bytearray(tt, data);
		Log.i("MCU", "fLASH: "+tt);
		byte[] tmp = new byte[15];
		tmp[0] = 0x01;
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (0x0b);
		System.arraycopy(COMM_MEM_WRITE, 0, tmp, 4, 2);
		tmp[6] = 0x00;
		System.arraycopy(addr, 0, tmp, 7, addr.length);
		System.arraycopy(data, 0, tmp, 11, data.length);
		return tmp;
	}
	
	
	public static byte[] getFlashCtrlPacket_1(byte[] data)
	{
		byte[] addr  = {0x10, 0x10, 0x00, 0x40};
		//byte[] value = {(byte) 0xEF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
		int tt = Utils.bytearray_to_int4(data);
		tt = tt | (1 << 2); 
		Utils.int4_to_bytearray(tt, data);
		Log.i("MCU", "fLASH: "+tt);
		byte[] tmp = new byte[15];
		tmp[0] = 0x01;
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (0x0b);
		System.arraycopy(COMM_MEM_WRITE, 0, tmp, 4, 2);
		tmp[6] = 0x00;
		System.arraycopy(addr, 0, tmp, 7, addr.length);
		System.arraycopy(data, 0, tmp, 11, data.length);
		return tmp;
	}
	
	
	public static byte[] getRemapMCUCtrlPacket_0(byte[] data)
	{
		byte[] addr  = {0x3c, 0x00, 0x0e, 0x40};
		//byte[] value = {(byte) 0xEF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
		int tt = Utils.bytearray_to_int4(data);
		tt = tt | (1 << 5); 
		Utils.int4_to_bytearray(tt, data);
		byte[] tmp = new byte[15];
		tmp[0] = 0x01;
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (0x0b);
		System.arraycopy(COMM_MEM_WRITE, 0, tmp, 4, 2);
		tmp[6] = 0x00;
		System.arraycopy(addr, 0, tmp, 7, addr.length);
		System.arraycopy(data, 0, tmp, 11, data.length);
		return tmp;
	}
	
	public static byte[] getRemapMCUCtrlPacket_1(byte[] data)
	{
		byte[] addr  = {0x3c, 0x00, 0x0e, 0x40};
		//byte[] value = {(byte) 0xEF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
		int tt = Utils.bytearray_to_int4(data);
		tt = (tt & 0xfffffff0) | 5 | (1 << 5);
		Utils.int4_to_bytearray(tt, data);
		byte[] tmp = new byte[15];
		tmp[0] = 0x01;
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (0x0b);
		System.arraycopy(COMM_MEM_WRITE, 0, tmp, 4, 2);
		tmp[6] = 0x00;
		System.arraycopy(addr, 0, tmp, 7, addr.length);
		System.arraycopy(data, 0, tmp, 11, data.length);
		return tmp;
	}
	
	
	public static byte[] getResetMCUCtrlPacket_0(byte[] data)
	{
		byte[] addr  = {0x3c, 0x00, 0x0e, 0x40};
		//byte[] value = {(byte) 0xEF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
		int tt = Utils.bytearray_to_int4(data);
		tt = tt & (~(1 << 4)); 
		Utils.int4_to_bytearray(tt, data);
		byte[] tmp = new byte[15];
		tmp[0] = 0x01;
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (0x0b);
		System.arraycopy(COMM_MEM_WRITE, 0, tmp, 4, 2);
		tmp[6] = 0x00;
		System.arraycopy(addr, 0, tmp, 7, addr.length);
		System.arraycopy(data, 0, tmp, 11, data.length);
		return tmp;
	}
	
	
	public static byte[] getResetMCUCtrlPacket_1()
	{
		byte[] addr  = {0x1c, 0x00, 0x0d, 0x40};
		byte[] value = {0x03, 0x00, 0x00, 0x00}; 
		byte[] tmp = new byte[15];
		tmp[0] = 0x01;
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (0x0b);
		System.arraycopy(COMM_MEM_WRITE, 0, tmp, 4, 2);
		tmp[6] = 0x00;
		System.arraycopy(addr, 0, tmp, 7, addr.length);
		System.arraycopy(value, 0, tmp, 11, value.length);
		return tmp;
	}
	
	public static byte[] getReadMEMCtrlPacket()
	{

		byte[] addr  = {0x3c, 0x00, 0x0e, 0x40};
		byte[] tmp = new byte[11];
		tmp[0] = 0x01;
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (0x07);
		System.arraycopy(COMM_MEM_READ, 0, tmp, 4, 2);
		tmp[6] = 0x04;
		System.arraycopy(addr, 0, tmp, 7, addr.length);
		
		return tmp;
	}
	
	public static byte[] getSendPWMCtrlPacket(byte[] data)
	{
		byte[] tmp = new byte[7+data.length];
		tmp[0] = 0x01;
		
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (3 + data.length);
		System.arraycopy(COMM_DRIVE_PWM_CTRL, 0, tmp, 4, 2);
		tmp[6] = (byte) data.length;
		System.arraycopy(data, 0, tmp, 7, data.length);
		return tmp;
	}
	
	public static byte[] getSendSppCtrlPacket(byte[] data)
	{
		byte[] tmp = new byte[7+data.length];
		tmp[0] = 0x01;
		
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (3 + data.length);
		System.arraycopy(COMM_BRIDGE_TX, 0, tmp, 4, 2);
		tmp[6] = (byte) data.length;
		System.arraycopy(data, 0, tmp, 7, data.length);
		return tmp;
	}
	
	public static byte[] getSendFmFrqCtrlPacket(byte[] data)
	{
		byte[] tmp = new byte[7+data.length];
		tmp[0] = 0x01;
		
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (3 + data.length);
		System.arraycopy(COMM_FM_FRQ_SET, 0, tmp, 4, 2);
		tmp[6] = (byte) data.length;
		System.arraycopy(data, 0, tmp, 7, data.length);
		return tmp;
	}
	
	
	public static byte[] getSendFmIniCtrlPacket(byte[] data)
	{
		byte[] tmp = new byte[7];
		tmp[0] = 0x01;
		
		System.arraycopy(opcode, 0, tmp, 1, 2);
		tmp[3] = (byte) (3);
		System.arraycopy(COMM_FM_FRQ_INI, 0, tmp, 4, 2);
		tmp[6] = (byte) 0;
		return tmp;
	}
	
	
}
 